package com.yulongtian.oneProblemEveryDay.month202303;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel/
 * 可
 *
 * @author yulongTian
 * @create 2023-03-05 9:09
 */
public class Test04 {
    public static void main(String[] args) {
        System.out.println(minOperationsMaxProfit(new int[]{8, 3}, 5, 6));
    }

    public static int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
        int sum = Arrays.stream(customers).sum();
        int turn = 0;//轮数
        int board_num = 0;//已登舱人数
        int wait_num = 0;//等待的人数
        int ans = Integer.MIN_VALUE;
        int best_turn = -1;
        //先看第一轮
        turn = 1;
        if (customers[0] >= 4) {
            board_num = 4;
            wait_num = customers[0] - 4;
            ans = board_num * boardingCost - turn * runningCost;
            best_turn = turn;
        } else {
            board_num = customers[0];
            ans = board_num * boardingCost - turn * runningCost;
            best_turn = turn;
        }
        while (board_num < sum) {
            turn++;
            if (turn - 1 < customers.length) {
                wait_num += customers[turn - 1];
                if (wait_num >= 4) {
                    board_num += 4;
                    wait_num -= 4;
                } else {
                    board_num += wait_num;
                    wait_num = 0;
                }
                if (ans < board_num * boardingCost - turn * runningCost) {
                    ans = board_num * boardingCost - turn * runningCost;
                    best_turn = turn;
                }
            } else {
                if (wait_num >= 4) {
                    board_num += 4;
                    wait_num -= 4;
                } else {
                    board_num += wait_num;
                    wait_num = 0;
                }
                if (ans < board_num * boardingCost - turn * runningCost) {
                    ans = board_num * boardingCost - turn * runningCost;
                    best_turn = turn;
                }
            }
        }
        return ans <= 0 ? -1 : best_turn;
    }

}
